#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

int main() {
    int fd[2];
    pipe(fd);

    pid_t pid = fork();
    if (pid == 0) {
        // 子进程：读数据
        close(fd[1]);  // 关闭写端
        char buf[100] = {0};
        int n = read(fd[0], buf, sizeof(buf));
        if (n > 0) {
            printf("Child received: %s\n", buf);
        }
        close(fd[0]);
    }
    else
    {
        close(fd[0]);
        char* msg = "hello from parent";
        write(fd[1], msg, strlen(msg));
        close(fd[1]);
        wait(NULL);
        }
        return 0;
}

